package com.exception.test2;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-09-19
 * Time: 21:49
 */
public class Student {
    private String name;
    private int age;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) throws Exception {
        if(sex.equals("男")||sex.equals("女")){
            this.sex = sex;
        }else{//非男非女
            //解决办法1：
            /*this.sex = "男";*/
            //解决办法2：给个友好型提示，但是打印结果为默认的null效果
            /*System.out.println("对不起，你的性别错误了");*/
            //解决办法3：
            //制造运行时异常： 运行异常也可以自己抛出自己解决或者交给JVM解决
            /*throw new RuntimeException("性别不对！");*/
            //制造检查异常
            /*try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }*/
            throw new Exception();
        }
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
    public Student() {
    }
    public Student(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        //this.sex = sex;
        //构造器的抛出的异常那个可以自己解决，也可以交给JVM解决
        try {
            this.setSex(sex);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
